﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Web.Infrastructure
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class BinaryControllerBase : MvcControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        protected FileStreamResult Execute(Stream stream)
        {
            return Execute(stream, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        protected FileStreamResult Execute(Stream stream, string contentType)
        {
            return Execute(stream, contentType, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="contentType"></param>
        /// <param name="fileDownloadName"></param>
        /// <returns></returns>
        protected virtual FileStreamResult Execute(Stream stream, string contentType, string fileDownloadName)
        {
            return Execute(stream, contentType, fileDownloadName, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="contentType"></param>
        /// <param name="fileDownloadName"></param>
        /// <param name="dispositionType"></param>
        /// <returns></returns>
        protected virtual FileStreamResult Execute(Stream stream, string contentType, string fileDownloadName, string dispositionType)
        {
            SetMediaContentHeader(contentType, fileDownloadName, dispositionType);
            var result = File(stream, contentType);
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected FileContentResult Execute(byte[] data)
        {
            return Execute(data, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        protected FileContentResult Execute(byte[] data, string contentType)
        {
            return Execute(data, contentType, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <param name="fileDownloadName"></param>
        /// <returns></returns>
        protected virtual FileContentResult Execute(byte[] data, string contentType, string fileDownloadName)
        {
            return Execute(data, contentType, fileDownloadName, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <param name="fileDownloadName"></param>
        /// <param name="dispositionType"></param>
        /// <returns></returns>
        protected virtual FileContentResult Execute(byte[] data, string contentType, string fileDownloadName, string dispositionType)
        {
            SetMediaContentHeader(contentType, fileDownloadName, dispositionType);
            var result = File(data, contentType);
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="fileDownloadName"></param>
        /// <param name="dispositionType"></param>
        protected virtual void SetMediaContentHeader(string contentType, string fileDownloadName, string dispositionType = null)
        {
            if (string.IsNullOrWhiteSpace(contentType))
            {
                contentType = MediaTypeNames.Application.Octet;
            }
            Response.Headers[HeaderNames.ContentDisposition] = CreateContentDisposition(fileDownloadName, dispositionType).ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileDownloadName"></param>
        /// <param name="dispositionType"></param>
        /// <returns></returns>
        protected ContentDisposition CreateContentDisposition(string fileDownloadName, string dispositionType)
        {
            dispositionType = string.IsNullOrEmpty(dispositionType) ? "inline" : dispositionType;
            var disposition = new ContentDisposition(dispositionType) { FileName = fileDownloadName };
            return disposition;
        }
    }
}
